//
//  GGTWeiBoDetailViewController.m
//  GGT
//
//  Created by Vision on 14-11-9.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTWeiBoDetailViewController.h"
#import "SBJson.h"
#import "GGTWBTableModel.h"
#import "GGTWeiBoDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface GGTWeiBoDetailViewController ()<WBHttpRequestDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *weiboTableView;
//model数组
@property (nonatomic, retain) NSMutableArray *modelArr;

@end

@implementation GGTWeiBoDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.modelArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //微博解析数据
    [WBHttpRequest requestWithAccessToken:[[NSUserDefaults standardUserDefaults] objectForKey:@"UserAccessToken"] url:@"https://api.weibo.com/2/statuses/home_timeline.json" httpMethod:@"GET" params:nil delegate:self withTag:@"1112"];
    self.view.backgroundColor = [UIColor whiteColor];
    self.weiboTableView  = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.weiboTableView.delegate = self;
    self.weiboTableView.dataSource = self;
    [self.view addSubview:self.weiboTableView];
    [self.weiboTableView registerClass:[GGTWeiBoDetailTableViewCell class] forCellReuseIdentifier:@"weiboTableView"];
    
//    NSDictionary *params = @{@"uid": [[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]};
}
//计算每个cell的大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat myHight = 0;
    CGFloat iconHight = 60; // 图标高度
    myHight +=iconHight;
    NSString *str = [[self.modelArr objectAtIndex:indexPath.row] weiboText];
    CGRect rect =  [str boundingRectWithSize:CGSizeMake(320, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    myHight += rect.size.height;
    if ([[self.modelArr[indexPath.row] pic_urls] count] == 0) {
        
    } else if ([[self.modelArr[indexPath.row] pic_urls] count] == 1) {
        myHight += 100;
    } else if ([[self.modelArr[indexPath.row] pic_urls] count] > 1 && [[self.modelArr[indexPath.row] pic_urls] count] <= 3){
        myHight += 70;
    } else if  ([[self.modelArr[indexPath.row] pic_urls] count] > 3 && [[self.modelArr[indexPath.row] pic_urls] count] <= 6){
        myHight +=74;
        myHight += 70;
    } else {
        myHight +=74;
        myHight += 74;
        myHight += 70;
    }
    NSString *str2 = [[self.modelArr objectAtIndex:indexPath.row] retweeted_text];
    CGRect rect2 =  [str2 boundingRectWithSize:CGSizeMake(320, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
       myHight += rect2.size.height;
    if ([[self.modelArr[indexPath.row] retweeted_pic_urls] count] == 0) {
        
    } else if ([[self.modelArr[indexPath.row] retweeted_pic_urls] count] == 1) {
        myHight += 100;
    } else if ([[self.modelArr[indexPath.row] retweeted_pic_urls] count] > 1 && [[self.modelArr[indexPath.row] retweeted_pic_urls] count] <= 3){
        myHight += 70;
    } else if  ([[self.modelArr[indexPath.row] retweeted_pic_urls] count] > 3 && [[self.modelArr[indexPath.row] retweeted_pic_urls] count] <= 6){
        myHight +=74;
        myHight += 70;
    } else {
        myHight +=74;
        myHight += 74;
        myHight += 70;
    }
 
    return myHight + 20;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GGTWeiBoDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weiboTableView"];
    GGTWBTableModel * temp = self.modelArr[indexPath.row];
    [cell.profile_image_urlView setImageWithURL:[NSURL URLWithString:temp.profile_image_url] placeholderImage:[UIImage imageNamed:@"hold.png"]];
    cell.nameLabel.text =  temp.name;
    cell.created_atLabel.text = temp.created_at;
    cell.sourceLabel.text = temp.source;
    cell.weiboTextLabel.text = temp.weiboText;
    cell.picArr = temp.pic_urls;
    cell.retweeted_text.text = temp.retweeted_text;
    cell.retweeted_picArr = temp.retweeted_pic_urls;
    return cell;
}
/**
 收到一个来自微博Http请求的响应
 
 @param response 具体的响应对象
 */

- (void)request:(WBHttpRequest *)request didReceiveResponse:(NSURLResponse *)response
{

}

/**
 收到一个来自微博Http请求失败的响应
 
 @param error 错误信息
 */

- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error
{

}

/**
 收到一个来自微博Http请求的网络返回
 
 @param result 请求返回结果
 */

- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result
{

}

/**
 收到一个来自微博Http请求的网络返回
 
 @param data 请求返回结果
 */

- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
{
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    id result = [parser objectWithData:data];
    for (NSDictionary *dic in [result objectForKey:@"statuses"]) {
        GGTWBTableModel *temp = [[GGTWBTableModel alloc]initWithDictionary:dic];
        [self.modelArr addObject:temp];
    }
    [self.weiboTableView reloadData];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
